package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.AppUtils;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenDocumentActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenRectifyAndReformActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenReviewActivity;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/10.
 */

public class SafeHiddenProcessTabTwoFragment extends BaseFragment {

    @Bind(R.id.one_btn)
    TextView oneBtn;
    @Bind(R.id.two_btn)
    TextView twoBtn;
    @Bind(R.id.three_btn)
    TextView threeBtn;
    @Bind(R.id.four_btn)
    TextView fourBtn;

    @Bind(R.id.hidden_handle_btn)
    Button hiddenHandleBtn;
    @Bind(R.id.view_line)
    View viewLine;
    @Bind(R.id.document_handle_btn)
    Button documentHandleBtn;
    @Bind(R.id.bottom_layout)
    LinearLayout bottomLayout;

    private static SafeHiddenDownloadVo safeHiddenDownloadVo;

    @Override
    protected void getBaseArguments() {

        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO)) {
                safeHiddenDownloadVo = (SafeHiddenDownloadVo) bundle.getSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO);
            }
        }
    }

    public static SafeHiddenProcessTabTwoFragment newInstance(SafeHiddenDownloadVo data) {

        Bundle args = new Bundle();
        args.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, data);

        SafeHiddenProcessTabTwoFragment fragment = new SafeHiddenProcessTabTwoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_process_tab_two;
    }

    @Override
    protected void initViews() {

        initCommon();
        transactionSupportFragment(SafeHiddenProcessTabTwoContentFragment.newInstance(safeHiddenDownloadVo));
    }

    private void initCommon() {

        if (SafeHiddenBo.Status.rectify.getValue().equals(safeHiddenDownloadVo.getStatus())
                || SafeHiddenBo.Status.overtime_rectify.getValue().equals(safeHiddenDownloadVo.getStatus())) { // 整 改 or 超期未整改

            twoBtn.setSelected(true);

            if (SafeHiddenBo.Status.rectify.getValue().equals(safeHiddenDownloadVo.getStatus())) {
                twoBtn.setText("整改");
            } else {
                twoBtn.setText("超期未整改");
            }

            if (AccountUtil.getUserID().equals(safeHiddenDownloadVo.getRectify_user_id())) {
                hiddenHandleBtn.setText("整 改");
                hiddenHandleBtn.setVisibility(View.VISIBLE);
                hiddenHandleBtn.setTag(SafeHiddenBo.Status.rectify.getValue());
            } else {
                hiddenHandleBtn.setVisibility(View.GONE);
            }


            //政府人员新建文书，但企业风险点不可以新建文书
            if (UserTypeUtil.isGovType(AccountUtil.getUserType())
                    && !SafeHiddenBo.SourceType.from_self_check_point.getValue().equals(safeHiddenDownloadVo.getSource_type())) {
                if (AccountUtil.getUserID().equals(safeHiddenDownloadVo.getRectify_user_id())) {
                    viewLine.setVisibility(View.VISIBLE);
                } else {
                    viewLine.setVisibility(View.GONE);
                }
                documentHandleBtn.setVisibility(View.VISIBLE);
            } else {
                documentHandleBtn.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
            }

        } else if (SafeHiddenBo.Status.review.getValue().equals(safeHiddenDownloadVo.getStatus())) {

            threeBtn.setSelected(true);
            if (AccountUtil.getUserID().equals(safeHiddenDownloadVo.getReview_user_id())) {
                hiddenHandleBtn.setText("复 查");
                hiddenHandleBtn.setVisibility(View.VISIBLE);
                hiddenHandleBtn.setTag(SafeHiddenBo.Status.review.getValue());
            } else {
                hiddenHandleBtn.setVisibility(View.GONE);
            }

            //政府人员新建文书，但企业风险点不可以新建文书
            if (UserTypeUtil.isGovType(AccountUtil.getUserType())
                    && !SafeHiddenBo.SourceType.from_self_check_point.getValue().equals(safeHiddenDownloadVo.getSource_type())) {
                if (AccountUtil.getUserID().equals(safeHiddenDownloadVo.getReview_user_id())) {
                    viewLine.setVisibility(View.VISIBLE);
                } else {
                    viewLine.setVisibility(View.GONE);
                }
                documentHandleBtn.setVisibility(View.VISIBLE);
            } else {
                documentHandleBtn.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
            }

        } else if (SafeHiddenBo.Status.finish.getValue().equals(safeHiddenDownloadVo.getStatus())) {

            fourBtn.setSelected(true);
            bottomLayout.setVisibility(View.GONE);

        }
    }

    private void transactionSupportFragment(Fragment fragment) {
        getChildFragmentManager().beginTransaction().replace(R.id.content_layout, fragment).commit();
    }


    @OnClick({R.id.hidden_handle_btn, R.id.document_handle_btn})
    public void onViewClicked(View view) {

        Bundle bundle = new Bundle();

        switch (view.getId()) {

            case R.id.hidden_handle_btn:

                if (AppUtils.isNetworkAvailable(getmContext())) {
                    bundle.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, safeHiddenDownloadVo);
                    if (SafeHiddenBo.Status.rectify.getValue().equals(hiddenHandleBtn.getTag())) {
                        UIHelper.showActivity(getmContext(), bundle, SafeHiddenRectifyAndReformActivity.class);
                    } else if (SafeHiddenBo.Status.review.getValue().equals(hiddenHandleBtn.getTag())) {
                        UIHelper.showActivity(getmContext(), bundle, SafeHiddenReviewActivity.class);
                    }
                } else {
                    mHUDUtil.showErrorTip("网络异常，请查看网络");
                }

                break;
            case R.id.document_handle_btn:

                if (AppUtils.isNetworkAvailable(getmContext())) {
                    bundle.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, safeHiddenDownloadVo);
                    UIHelper.showActivity(getmContext(), bundle, SafeHiddenDocumentActivity.class);
                } else {
                    mHUDUtil.showErrorTip("网络异常，请查看网络");
                }

                break;
        }
    }
}
